package tmt.usercenter.web.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.tmt.annotation.Select2OptionItem;
import com.tmt.annotation.Select2OptionValue;
import com.tmt.jpa.jpaservice.SyncUpdate;
import com.tmt.jpafieldvalueconvert.JpaFieldValueConvertListener;
import com.voodoodyne.jackson.jsog.JSOGGenerator;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * 可授权范围
 */
@Getter
@Setter
@Entity
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners({JpaFieldValueConvertListener.class, AuditingEntityListener.class})
@JsonIdentityInfo(generator = JSOGGenerator.class)
public class AuthScope extends BaseCreator implements Serializable {

	@Select2OptionItem
	@Column(nullable = false)
	private String name;

	@Select2OptionItem(order = 1)
	@Column(nullable = false, unique = true)
	private String scope;

	@Column(nullable = false)
	private String description;

	@Column(nullable = false)
	private int sort;

	@Select2OptionItem(booleanConverter = {"系统授权范围", "客户授权范围"})
	@Column(nullable = false)
	private boolean systemScope = true;

	@ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
	@JoinColumn(name="clientSystem_id")
	private ClientSystem clientSystem;

	@Select2OptionValue("authResources[].id")
	@OneToMany(mappedBy = "authScope", cascade = {CascadeType.REFRESH})
	private List<AuthResource> authResources;

	/**
	 * 获得本授权范围访问权限的客户系统集合。
	 */
	@OneToMany(mappedBy = "authScope", cascade = {CascadeType.REFRESH})
	private List<ClientSystemScope> clientSystemScopes;

}
